//
//  HTWelcomeViewController.swift
//  mySinaBlog
//
//  Created by whtapple on 3/25/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import UIKit
import SDWebImage

class HTWelcomeViewController: UIViewController {
    
    var iconBottomCon: NSLayoutConstraint?
    override func viewDidLoad() {

        super.viewDidLoad()
       
        prepareUI()
         loadiconImage()
    }
    /// 加载头像
    private func loadiconImage(){
        if let urlstring = HTUserAccountViewModel.sharedInstance.userAccount?.avatar_large{
            self.iconView.sd_setImageWithURL(NSURL(string: urlstring), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        HTUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
                print("\(error)")
            }
            if let urlstring = HTUserAccountViewModel.sharedInstance.userAccount?.avatar_large{
                self.iconView.sd_setImageWithURL(NSURL(string: urlstring), placeholderImage: UIImage(named: "avatar_default_big"))
            }
        }
    }
    //准备 UI
    private func prepareUI(){
        ///添加子控件
        view.addSubview(bgView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        ///设置约束
        //去掉自动布局
//        bgView.translatesAutoresizingMaskIntoConstraints = false
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        ///约束
        //背景图片约束
        bgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
//        view.addConstraint(NSLayoutConstraint(item: bgView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bgView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bgView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: bgView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        //图标约束
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        iconBottomCon = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160)
//        view.addConstraint(iconBottomCon!)
        iconView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSizeMake(85, 85))
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
        }
        //label 约束
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.bottom.equalTo(iconView).offset(20)
        }
    }
    //MARK: -动画
    override func viewDidAppear(animated: Bool) {
        self.iconView.snp_updateConstraints(closure: { (make) -> Void in
            make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.size.height - 160))
        })
//        iconBottomCon?.constant = -(UIScreen.mainScreen().bounds.size.height - 160)
        UIView.animateWithDuration(2, delay: 0.5, usingSpringWithDamping: 0.3, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.welcomeLabel.alpha = 1
                }, completion: { (_) -> Void in
                   AppDelegate.delegateChangeController(HTmainViewController())
            })
        }
    }
    //MARK: -懒加载
    //背景图片
    private lazy var bgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //图标 裁剪成圆形
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    //欢迎 Label
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        label.text = "welcomQian"
        label.textAlignment  = NSTextAlignment.Center
        label.sizeToFit()
        label.font = UIFont.systemFontOfSize(14)
        label.alpha = 0
        return label
    }()
    

}
